package com.zhang.admin.controller;

import com.zhang.admin.dto.MenuTree;
import com.zhang.admin.po.SysElement;
import com.zhang.admin.service.ElementService;
import com.zhang.admin.util.TreeUtil;
import com.zhang.admin.vo.CreateUpdateElementParams;
import com.zhang.common.core.constant.CommonConstant;
import com.zhang.common.core.constant.MsgTemplate;
import com.zhang.common.core.vo.Result;
import com.zhang.common.log.annotation.Log;
import com.zhang.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zsw
 * @Date 2019/11/19 11:13
 * @Version V1.0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/menu")
public class MenuController {
    private final ElementService elementService;

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单,所有的
     */
    @GetMapping(value = "/tree")
    public Result<List<MenuTree>> getTree() {
        return Result.success(TreeUtil.buildTree(elementService.queryAll(), -1));
    }

    /**
     * 返回角色的菜单集合
     *
     * @param roleId 角色ID
     * @return 属性集合
     */
    @GetMapping("/tree/{roleId}")
    public List getRoleTree(@PathVariable Integer roleId) {
        return elementService.queryElementByRoleId(roleId)
                .stream()
                .map(SysElement::getElementId)
                .collect(Collectors.toList());
    }

    /**
     * 返回当前用户的树形菜单集合
     *
     * @return 当前用户的树形菜单
     */
    @GetMapping
    public Result getUserMenu() {
        // 获取符合条件的菜单
        User user = SecurityUtils.getUser();

        List<SysElement> all = elementService.queryElementByUsername(user.getUsername());

        List<MenuTree> menuTreeList = all.stream()
                .filter(menuVo -> CommonConstant.MENU==menuVo.getType())
                .map(MenuTree::new)
                .sorted(Comparator.comparingInt(MenuTree::getSort))
                .collect(Collectors.toList());
        return Result.success(TreeUtil.buildByLoop(menuTreeList, -1));
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public Result<SysElement> queryEleMentById(@PathVariable Integer id) {
        return Result.success(elementService.queryEleMentById(id));
    }

    /**
     * 新增菜单
     *
     * @param createUpdateElementParams 菜单信息
     * @return success/false
     */
    @Log(message = "'创建菜单 [ '+sysElement.name +' ]'")
    @PostMapping
    @PreAuthorize("hasAuthority('menu:add')")
    public Result save(@Valid CreateUpdateElementParams createUpdateElementParams) {
        elementService.createUpdateElement(createUpdateElementParams);
        return Result.success(MsgTemplate.RESULT_SAVE_OK);
    }

    /**
     * 更新菜单
     *
     * @param sysMenu
     * @return
     */
    @Log(message = "'更新菜单 [ '+sysElement.name +' ]'")
    @PutMapping
    @PreAuthorize("hasAuthority('menu:update')")
    public Result update(@Valid CreateUpdateElementParams createUpdateElementParams) {
        elementService.createUpdateElement(createUpdateElementParams);
        return Result.success(MsgTemplate.RESULT_UPDATE_OK);
    }

    @Log(message = "'删除菜单 [ '+sysElement.name +' ]'")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('menu:delete')")
    public R removeById(@PathVariable Integer id) {
        return sysMenuService.removeMenuById(id);
    }


}